import { ElMessage } from 'element-plus'

const state = {
  categories: [],
  applications: [],
  loading: false
}

const mutations = {
  SET_CATEGORIES: (state, categories) => {
    state.categories = categories
  },
  SET_APPLICATIONS: (state, applications) => {
    state.applications = applications
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  ADD_CATEGORY: (state, category) => {
    state.categories.push(category)
  },
  UPDATE_APPLICATION_STATUS: (state, { id, status }) => {
    const application = state.applications.find(a => a.id === id)
    if (application) {
      application.status = status
    }
  }
}

const actions = {
  // 获取所有IP分类
  async getCategories({ commit, rootState }) {
    try {
      commit('SET_LOADING', true)
      // 模拟API调用
      const categories = [] // 这里应该是实际的API调用
      
      // 商户只能看到已审核通过的分类
      if (rootState.user.roles.includes('merchant')) {
        return categories.filter(c => c.status === 'approved')
      }
      
      // 管理员可以看到所有分类
      commit('SET_CATEGORIES', categories)
      return categories
    } catch (error) {
      ElMessage.error('获取IP分类失败')
      return []
    } finally {
      commit('SET_LOADING', false)
    }
  },

  // 商户申请新IP分类
  async applyCategory({ commit, rootState }, categoryData) {
    try {
      if (!rootState.user.roles.includes('merchant')) {
        throw new Error('只有商户可以申请新IP分类')
      }

      // 模拟API调用
      const application = {
        ...categoryData,
        merchantId: rootState.user.merchantId,
        status: 'pending',
        createTime: new Date().toISOString()
      }

      // 这里应该是实际的API调用
      commit('SET_APPLICATIONS', [...state.applications, application])
      ElMessage.success('IP分类申请已提交，请等待审核')
      return application
    } catch (error) {
      ElMessage.error(error.message || 'IP分类申请失败')
      throw error
    }
  },

  // 管理员审核IP分类申请
  async reviewCategoryApplication({ commit, rootState }, { applicationId, approved, reason }) {
    try {
      if (!rootState.user.roles.includes('admin')) {
        throw new Error('只有管理员可以审核IP分类申请')
      }

      // 模拟API调用
      const status = approved ? 'approved' : 'rejected'
      
      // 这里应该是实际的API调用
      commit('UPDATE_APPLICATION_STATUS', { id: applicationId, status })
      
      if (approved) {
        const application = state.applications.find(a => a.id === applicationId)
        if (application) {
          const newCategory = {
            ...application,
            status: 'active',
            approvedTime: new Date().toISOString()
          }
          commit('ADD_CATEGORY', newCategory)
        }
      }

      ElMessage.success(`IP分类申请已${approved ? '通过' : '拒绝'}`)
    } catch (error) {
      ElMessage.error(error.message || '审核IP分类申请失败')
      throw error
    }
  },

  // 获取IP分类申请列表
  async getCategoryApplications({ commit, rootState }) {
    try {
      commit('SET_LOADING', true)
      // 模拟API调用
      const applications = [] // 这里应该是实际的API调用
      
      // 商户只能看到自己的申请
      if (rootState.user.roles.includes('merchant')) {
        return applications.filter(a => a.merchantId === rootState.user.merchantId)
      }
      
      // 管理员可以看到所有申请
      commit('SET_APPLICATIONS', applications)
      return applications
    } catch (error) {
      ElMessage.error('获取IP分类申请列表失败')
      return []
    } finally {
      commit('SET_LOADING', false)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 